﻿<?php
	include_once('object/Person.php');
	include_once('object/CustomGroup.php');
	include_once('object/PublicGroup.php');
	
	session_start();
/**
 * Get User Information
 */
	class userInfo{	
		 var $user_id;
	    /**
	     * Initial UserInformation And Create DataBase Connection
	     */	
		 function userInfo(){
//create database initial
			mysql_connect("localhost", "root","3210021");
			mysql_select_db("im");
			mysql_query("SET NAMES UTF8");
			
//get user_id session
			$this->user_id = $_SESSION['uid'];
		}
	    /**
	     * checkLogin method
	     * @returns a array persion
	     */
		public function checkLogin($username,$password,$stats){
			$password = md5($password);
			
			$sql = "SELECT `uid`,`password`,`username` FROM `cdb_members` WHERE `username` ='".$username."'";
			$result = mysql_query($sql);
			$row = mysql_fetch_array($result,MYSQL_ASSOC);
			$pwd = $row['password'];
			$uid = $row['uid'];
			
			if($password == $pwd){
				$_SESSION['uid'] = $uid;
				$this->user_id = $uid;
				$_SESSION['stats'] = $stats;
				
				$_SESSION['user_name'] = $row['username'];
				return $this->user_id;
			}else{
				return 'ERROR';
			}
		}
	    /**
	     * getUserInfo method
	     * @returns a array persion
	     */
		public function getUserInfo(){
			$person = new Person();
			
			$sql = 'SELECT m.*,mf.* from cdb_members m
			LEFT JOIN cdb_memberfields mf ON mf.uid=m.uid where m.uid='.$this->user_id;
			
			$result = mysql_query($sql);
			$row = mysql_fetch_array($result,MYSQL_ASSOC);
//insert object 
			$person->username = $row['username'];
			$person->email = $row['email'];
			$person->bday = $row['bday'];
			$person->gender = $row['gender'];
			$person->bio = split('			',$row['bio']);
			$person->bio = $person->bio[0];
			$person->sightml = $row['sightml'];
			$person->site = $row['site'];
			$person->location = $row['location'];
			$person->qq = $row['qq'];
			$person->invisible = $row['invisible'];
			$person->avatar = $row['avatar'];
			if($person->avatar == ""){
				$person->avatar = "http://192.168.1.126:8000/discuz/images/avatars/noavatar.gif";
			}else{
				$person->avatar = "http://192.168.1.126:8000/discuz/".$row['avatar'];
			}
			$person->unreadmail = 20;
			$person->mailPath = "http://mail.163.com/";
			
			$_SESSION['avatar'] = $person->avatar;
			
			mysql_close();
			return $person;
		}
	    /**
	     * userMInfo method
	     * @returns a array persion
	     */
		public function userMInfo($uid){
			$person = new Person();
			
			$sql = 'SELECT m.* from cdb_members m
			LEFT JOIN cdb_memberfields mf ON mf.uid=m.uid where m.uid='.$uid;
			
			$result = mysql_query($sql);
			$row = mysql_fetch_array($result,MYSQL_ASSOC);
//insert object 
			$person->username = $row['username'];
			$person->email = $row['email'];
			$person->bday = $row['bday'];
			$person->gender = $row['gender'];
			$person->bio = split('			',$row['bio']);
			$person->bio = $person->bio[0];
			$person->sightml = $row['sightml'];
			$person->site = $row['site'];
			$person->location = $row['location'];
			$person->qq = $row['qq'];
			$person->invisible = $row['invisible'];
			$person->avatar = $row['avatar'];
			if($person->avatar == ""){
				$person->avatar = "http://192.168.1.126:8000/discuz/images/avatars/noavatar.gif";
			}else{
				$person->avatar = "http://192.168.1.126:8000/discuz/".$row['avatar'];
			}
			
			mysql_close();
			return $person;
		}
	    /**
	     * getFriendList method
	     * @returns a XML FrientList
	     */
		public function getFriendList(){
            $sql ='SELECT DISTINCT cg.groupname,cg.groupid
					FROM  `cdb_customgroup` cg where cg.uid='.$this->user_id;
					
            $result = mysql_query($sql);
			
			$g = array();
			while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
				$group = new CustomGroup();
				$group->data = $row['groupid'];
				$group->gname = $row['groupname'];
				$group->type = "root";
			 	
				$xmlstr = $xmlstr."<node label='$group->gname' icon='tran' type='$group->type' data='$group->data' ms=''>";
				
				$sql1 = "SELECT m. *
							FROM cdb_members m
							LEFT JOIN cdb_memberfields mf ON mf.uid = m.uid
							WHERE m.uid
							IN (SELECT buddyid FROM  `cdb_customgpmember` WHERE  `uid` =$this->user_id AND `groupid` =$group->data and `cp_type`=3)";
				$result1 = mysql_query($sql1);
				$p = array();
				while ($row1 = mysql_fetch_array($result1,MYSQL_ASSOC)){
//Format data to object 
					$person = new Person();

					$person->username = $row1['username'];
					$person->email = $row1['email'];
					$person->bday = $row1['bday'];
					$person->gender = $row1['gender'];
					$person->bio = split('			',$row1['bio']);
					$person->bio = $person->bio[0];
					$person->sightml = $row1['sightml'];
					$person->site = $row1['site'];
					$person->location = $row1['location'];
					$person->qq = $row1['qq'];
					$person->ms = 'red';
					$person->invisible = $row1['invisible'];
					$person->uid = $row1['uid'];

					switch($person->invisible){
						case 0:
						$person->icon = "online";
						break;
						case 1:
						$person->icon = "hide";
						break;
						case 2:
						$person->icon = "busy";
						break;
						case 3:
						$person->icon = "leave";
						break;
						
						default:
						$person->icon = "online";
						break;
					}

					$p[] = $person;
					$xmlstr = $xmlstr."<node label='$person->username' icon='$person->icon' type='$group->data' data='$person->uid' parent='$group->data' ms=''></node>";
				}
				
				$group->children = $p;
				$g[] = $group;
				
				$xmlstr = $xmlstr."<node icon='tran' data='tran'/></node>";
			}
			$sql2 = "SELECT m.*  
						FROM cdb_members m
						LEFT JOIN cdb_memberfields mf ON mf.uid = m.uid
						WHERE m.uid
						IN (SELECT buddyid FROM  `cdb_customgpmember` WHERE  `uid` =$this->user_id AND `groupid` =0 and `cp_type`=3)";
			$result2 = mysql_query($sql2);
			$p = array();
			while ($row1 = mysql_fetch_array($result2,MYSQL_ASSOC)){
//Format data to object 
				$person = new Person();

				$person->username = $row1['username'];
				$person->email = $row1['email'];
				$person->bday = $row1['bday'];
				$person->gender = $row1['gender'];
				$person->bio = split('			',$row1['bio']);
				$person->bio = $person->bio[0];
				$person->sightml = $row1['sightml'];
				$person->site = $row1['site'];
				$person->location = $row1['location'];
				$person->qq = $row1['qq'];
				$person->ms = 'red';
				$person->invisible = $row1['invisible'];
				$person->uid = $row1['uid'];
				
				switch($person->invisible){
						case 0:
						$person->icon = "online";
						break;
						case 1:
						$person->icon = "hide";
						break;
						case 2:
						$person->icon = "busy";
						break;
						case 3:
						$person->icon = "leave";
						break;
						
						default:
						$person->icon = "online";
						break;
					}

				$g[] = $person;
				$xmlstr = $xmlstr."<node label='$person->username' icon='$person->icon' data='$person->uid' type='children' parent='0' ms=''></node>";
				}
				$xmlstr = $xmlstr."<node label='临时联系人' icon='tran' type='root' data='-1' ms=''><node icon='tran' data='tran'/></node>";
			mysql_close();
			return $xmlstr;
		}
	    /**
	     * getUnAddFriendList method
	     * @returns a Array XML FrientList
	     */
		public function getUnAddFriendList(){
			$sql ="SELECT `groupid` , `buddyid`
					FROM `cdb_customgpmember`
					WHERE `uid` =$this->user_id
					AND `cp_type` =2";
			$result = mysql_query($sql);
			
			$unAddArray = array();
			$g = array();
			while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
				$sql1 = 'SELECT m.*,mf.* from cdb_members m
				LEFT JOIN cdb_memberfields mf ON mf.uid=m.uid where m.uid='.$row['buddyid'];
				
				$result1 = mysql_query($sql1);
				$row1 = mysql_fetch_array($result1,MYSQL_ASSOC);
	//insert object 
				$person->username = $row1['username'];
				$person->email = $row1['email'];
				$person->bday = $row1['bday'];
				$person->gender = $row1['gender'];
				$person->bio = split('			',$row1['bio']);
				$person->bio = $person->bio[0];
				$person->sightml = $row1['sightml'];
				$person->site = $row1['site'];
				$person->location = $row1['location'];
				$person->qq = $row1['qq'];
				$person->invisible = $row1['invisible'];
				$person->avatar = $row1['avatar'];
				if($person->avatar == ""){
					$person->avatar = "http://192.168.1.126:8000/discuz/images/avatars/noavatar.gif";
				}else{
					$person->avatar = "http://192.168.1.126:8000/discuz/".$row1['avatar'];
				}
				$person->uid = $row['buddyid'];
				
				$sql2 = " UPDATE `cdb_customgpmember` SET `cp_type` = '3' WHERE `uid` =$this->user_id AND `buddyid` =$person->uid AND `cp_type` =2 ";
				$result2 = mysql_query($sql2);
				
				switch($person->invisible){
						case 0:
						$person->icon = "online";
						break;
						case 1:
						$person->icon = "hide";
						break;
						case 2:
						$person->icon = "busy";
						break;
						case 3:
						$person->icon = "leave";
						break;
						
						default:
						$person->icon = "online";
						break;
					}
				
				$unAddArray[] = "<node label='$person->username' icon='$person->icon' type='children' data='$person->uid' parent='".$row['groupid']."' ms=''/>";
			}
			
			return $unAddArray;
		}
	    /**
	     * getAddedMessage method
	     * @returns a Array AddedMessgage
	     */
		public function getAddedMessage(){
			$sql = "SELECT DISTINCT `uid` FROM `cdb_customgpmember` where `uid` NOT IN (select `buddyid` from `cdb_customgpmember` where `uid`=$this->user_id AND `cp_type` =3)  AND `buddyid`=$this->user_id  AND `cp_type` =0";
			$result = mysql_query($sql);
			
			$p = array();
			while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
				$p[] = $row['uid'];
				$uid = $row['uid'];
				$sql1 = " UPDATE `cdb_customgpmember` SET `cp_type` = '1' WHERE `uid` =$uid AND `buddyid` =$this->user_id AND `cp_type` =0 ";
				$result1 = mysql_query($sql1);
			}
			
			mysql_close();
			
			if(count($p) >0){
				return $p;
			}else {
				return 'error';
			}
		}
	    /**
	     * getGroupList method
	     * @returns a array FrientList
	     */
		public function getGroupList(){
			$sql = "SELECT DISTINCT pg.p_gid,pg.`p_gname` FROM `cdb_publicgroup` pg LEFT JOIN cdb_publicgmember pgm  ON pg.p_gid=pgm.pgid  where pg.p_regman=$this->user_id OR pgm.uid =$this->user_id";
			$result = mysql_query($sql);
			
			$pgroup = new PublicGroup();
			
			while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
				$pgroup->groupid = $row['p_gid'];
				$pgroup->groupname = $row['p_gname'];
//				$pgroup->groupdetail = $row['p_gdetail'];
//				$pgroup->grouppersons = $row['p_gpersons'];
//				$pgroup->regman = $row['p_regman'];
//				$pgroup->regtime = $row['p_regtime'];
//				$pgroup->status = $row['p_status'];
				
				$xmlstr = $xmlstr."<node label='$pgroup->groupname' icon='group' data='$pgroup->groupid' ms='' type='root' msnum='' >";
					$sql1 = "SELECT m. *
								FROM cdb_members m
								LEFT JOIN cdb_memberfields mf ON mf.uid = m.uid
								WHERE m.uid
								IN (SELECT uid FROM  `cdb_publicgmember` WHERE  `pgid` =$pgroup->groupid)";
					$result1 = mysql_query($sql1);
					while ($row1 = mysql_fetch_array($result1, MYSQL_ASSOC)) {
						$person = new Person();
						$person->username = $row1['username'];
						$person->email = $row1['email'];
						$person->bday = $row1['bday'];
						$person->gender = $row1['gender'];
						$person->bio = split('			',$row1['bio']);
						$person->bio = $person->bio[0];
						$person->sightml = $row1['sightml'];
						$person->site = $row1['site'];
						$person->location = $row1['location'];
						$person->qq = $row1['qq'];
						$person->ms = 'red';
						$person->invisible = $row1['invisible'];
						$person->uid = $row1['uid'];
	
					switch($person->invisible){
						case 0:
						$person->icon = "online";
						break;
						case 1:
						$person->icon = "hide";
						break;
						case 2:
						$person->icon = "busy";
						break;
						case 3:
						$person->icon = "leave";
						break;
						
						default:
						$person->icon = "online";
						break;
					}
						
						$xmlstr = $xmlstr."<node label='$person->username' icon='$person->icon' parent='$pgroup->groupid' type='children' data='$person->uid' ms='red'></node>";
					}
				$xmlstr = $xmlstr."</node>";
			}
			
			return $xmlstr;
		}
	    /**
	     * changeStatus method
	     * @returns 1(changed) or 0(unchanged) 
	     */
		public function changeStatus($status){
            $sql = "update `cdb_members`  set `invisible`=".$status." where uid=".$this->user_id;
			$result = mysql_query($sql);
			
			mysql_close();
			
			if($result){
				return $status;
			}else {
				return 'error';
			}
		}	
	    /**
	     * Control Class
	     * add Custom Group  method
	     * @returns 1(added) or 0(unadded) 
	     */
		public function addCustomGroup($groupname){
			$sql = "INSERT INTO `cdb_customgroup` (`uid` ,`groupid` ,`groupname`)
					VALUES ('$this->user_id',  '',  '$groupname');";
			$result = mysql_query($sql);
			
			mysql_close();
			
			if($result){
				return $groupname;
			}else {
				return "error";
			}
		}
	    /**
	     * addFriend method
	     * @returns add result ok(error)
	     */
		public function addFriend($friendID,$groupID){
			$sql = "INSERT INTO  `cdb_customgpmember` (`uid` ,`groupid` ,`buddyid`,'cp_type')
					VALUES ('$this->user_id',  '$groupID',  '$friendID','1');";
				$result = mysql_query($sql);
				
				mysql_close();
				
				if($result){
					return "ok";
				}else {
					return "error";
				}
		}
	    /**
	     * addSingleFriend method
	     * @returns add result ok(error)
	     */
		public function addSingleFriend($friendID,$type,$gid){
			if($gid == null){
				$gid = 0;
			}
			if($type == 0){
				$sql = "SELECT `uid` FROM `cdb_customgpmember` WHERE `uid`='$this->user_id' AND `buddyid`=$friendID";
				$result = mysql_query($sql);
				
				$row = mysql_fetch_array($result, MYSQL_ASSOC);
				
				if($row['uid'] == null){
					$sql = "INSERT INTO `cdb_customgpmember` (`uid` ,`groupid` ,`buddyid`,`cp_type`)
							VALUES ('$this->user_id',  '$gid',  '$friendID','$type');";
				}else{
					$sql = "UPDATE `cdb_customgpmember` SET `cp_type` = '0' WHERE (`uid` =$this->user_id AND `buddyid` =$friendID) OR (`buddyid` =$this->user_id AND `uid` =$friendID) ";
				}
				$result = mysql_query($sql);
			}else if($type == 1){
				$sql = "SELECT `uid` FROM `cdb_customgpmember` WHERE `uid`='$this->user_id' AND `buddyid`=$friendID";
				$result = mysql_query($sql);
				
				$row = mysql_fetch_array($result, MYSQL_ASSOC);
				
				if($row['uid'] == null){
					$sql = "INSERT INTO `cdb_customgpmember` (`uid` ,`groupid` ,`buddyid`,`cp_type`)
							VALUES ('$this->user_id',  '$gid',  '$friendID','$type');";
					$result = mysql_query($sql);
						
					$sql = " UPDATE `cdb_customgpmember` SET `cp_type` = '2' WHERE (`uid` =$this->user_id AND `buddyid` =$friendID) OR (`buddyid` =$this->user_id AND `uid` =$friendID) AND `cp_type` =1 ";
					$result = mysql_query($sql);
				}else{
					$sql = " UPDATE `cdb_customgpmember` SET `cp_type` = '2' WHERE (`uid` =$this->user_id AND `buddyid` =$friendID) OR (`buddyid` =$this->user_id AND `uid` =$friendID) AND `cp_type` =1 ";
					$result = mysql_query($sql);
				}
			}

			mysql_close();
			
			if($result){
				return "ok";
			}else {
				return "error";
			}
		}
	    /**
	     * findFriendByID method
	     * @returns add result ok(error)
	     */
		public function findFriendByID($uid){
			$userArray = array();
			$userObject = array();
			$sql = 'SELECT m.*,mf.* from cdb_members m
			LEFT JOIN cdb_memberfields mf ON mf.uid=m.uid where m.uid='.$uid.' order by m.uid DESC';

			$result = mysql_query($sql);
			
			while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
					$userObject['uid'] = $uid;
					$userObject['username'] = $row['username'];
					$userObject['nickname'] = $row['nickname'];
					$userObject['location'] = $row['location'];
					if($row['invisible'] == 0){
						$userObject['invisible'] = "离线";
					}else{
						$userObject['invisible'] = '在线';
					}
					array_unshift($userArray,$userObject);
				}
			if(count($userArray) == 0){
				return 'nofind';
			}else {
				return $userArray;
			}
		}
	    /**
	     * findFriendByName method
	     * @returns add result ok(error)
	     */
		public function findFriendByName($name){
			$sql = "SELECT m.uid from cdb_members m
			LEFT JOIN cdb_memberfields mf ON mf.uid=m.uid where m.username='$name' order by m.uid DESC";

			$result = mysql_query($sql);
			
			$row = mysql_fetch_array($result, MYSQL_ASSOC);
			if($row['uid'] == null){
				return 'no';
			}else {
				return $row['uid'];
			}
		}
	    /**
	     * findFriend method
	     * @returns add result ok(error)
	     */
		public function findFriend($uid,$name){
			$userArray = array();
			$userObject = array();
			$sql = "SELECT m.uid,m.username,m.invisible,mf.nickname,mf.location from cdb_members m
			LEFT JOIN cdb_memberfields mf ON mf.uid=m.uid where m.uid='$uid' or m.username='$name' order by m.uid DESC";

			$result = mysql_query($sql);
			
			while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
					$userObject['uid'] = $row['uid'];
					$userObject['username'] = $row['username'];
					$userObject['nickname'] = $row['nickname'];
					$userObject['location'] = $row['location'];
					
					if($row['invisible'] == 0){
						$userObject['invisible'] = "离线";
					}else{
						$userObject['invisible'] = '在线';
					}
					array_unshift($userArray,$userObject);
				}
			if(count($userArray) == 0){
				return 'nofind';
			}else {
				return $userArray;
			}
		}
	    /**
	     * findGroup method
	     * @returns add result ok(error)
	     */
		public function findGroup($gname){
			$gArray = array();
			$gObject = array();
			$sql = "SELECT `p_gid`,`p_gname`,`p_gdetail` FROM `cdb_publicgroup` where `p_gname`='$gname'";

			$result = mysql_query($sql);
			
			while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
					$gObject['p_gid'] = $row['p_gid'];
					$gObject['p_gname'] = $row['p_gname'];
					$gObject['p_gdetail'] = $row['p_gdetail'];

					array_unshift($gArray,$gObject);
				}
			if(count($gArray) == 0){
				return 'nofind';
			}else {
				return $gArray;
			}
		}
	    /**
	     * findFriendAll method
	     * @returns add result ok(error)
	     */
		public function findFriendAll($uid,$name){
			$userArray = array();
			$userObject = array();
			$sql = "SELECT m.*,mf.* from cdb_members m
			LEFT JOIN cdb_memberfields mf ON mf.uid=m.uid where m.uid='$uid' or m.username='$name' order by m.uid DESC";

			$result = mysql_query($sql);
			
			while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
					$userObject['uid'] = $row['uid'];
					$userObject['username'] = $row['username'];
					$userObject['nickname'] = $row['nickname'];
					$userObject['location'] = $row['location'];
					if($row['invisible'] == 0){
						$userObject['invisible'] = "离线";
					}else{
						$userObject['invisible'] = '在线';
					}
					array_unshift($userArray,$userObject);
				}
			if(count($userArray) == 0){
				return 'nofind';
			}else {
				return $userArray;
			}
		}
	    /**
	     * see userMessage method
	     * @returns add result ok(error)
	     */
		public function userMessage($uid){
			$userArray = array();
			
			$sql = 'SELECT m.*,mf.* from cdb_members m
			LEFT JOIN cdb_memberfields mf ON mf.uid=m.uid where m.uid='.$uid;

			$result = mysql_query($sql);
			$row = mysql_fetch_array($result,MYSQL_ASSOC);
			
			foreach($row as $k =>$str){
				$userArray[$k] = trim($str);
			}
			if(count($userArray) == 0){
				return 'no';
			}else {
				switch ($userArray['gender'].$userArray['invisible']){
					case 10:
					$userArray['gender'] = 'myMan';
					break;
					case 20:
					$userArray['gender'] = 'myWoman';
					break;
					case 11:
					$userArray['gender'] = 'myManleave';
					break;
					case 12:
					$userArray['gender'] = 'myWomanleave';
					break;
            		case 00:
            		$userArray['gender'] = 'other';
            		break;
            		case 01:
            		$userArray['gender'] = 'otherhide';
            		break;
				}
				$userArray['stats'] = $_SESSION['stats'];
				return $userArray;
			}
		}
	    /**
	     * deleFriend method
	     * @returns add result ok(error)
	     */
	    public function  deleteFriend($fid){
	    	//$sql = "DELETE FROM cdb_customgpmember WHERE  `uid` =$this->user_id AND `buddyid` =$fid";
	    	$sql = "DELETE FROM cdb_customgpmember WHERE  (`uid` =$this->user_id OR `uid` =$fid) AND  (`buddyid` =$fid OR `buddyid`=$this->user_id)";
			$result = mysql_query($sql);
				
			mysql_close();
				
			if($result){
				return "ok";
			}else {
				return "error";
			}
	    }
		/**
	     * deleGroupFriend method
	     * @returns add result ok(error)
	     */
	    public function  deleGroupFriend($fid,$gid){
	    	$sql ="SELECT `p_regman` FROM `cdb_publicgroup` WHERE `p_gid` =$gid";
	    	$result = mysql_query($sql);
	    	$row = mysql_fetch_array($result,MYSQL_ASSOC);
	    	
	    	if($row['p_regman'] == $this->user_id && $fid != $this->user_id){
		    	$sql = "DELETE FROM `cdb_publicgmember` WHERE `cdb_publicgmember`.`pgid` =$gid AND `uid` =$fid LIMIT 1";
				$result = mysql_query($sql);
				
	    		if($result){
					return "ok";
				}else {
					return "error";
				}
				mysql_close();
	    	}else{
	    		mysql_close();
	    		return "nopower";
	    	}	
	    }
	    /**
	     * changeGroupName method
	     * @returns add result ok(error)
	     */
		public function changeGroupName($newName,$cgroupid){
			$sql = "UPDATE `cdb_customgroup` SET  `groupname` =  '$newName' WHERE `groupid` ='$cgroupid' AND `uid`='$this->user_id' ;";
			$result = mysql_query($sql);
			
			mysql_close();
			
			if($result){
				return "ok";
			}else {
				return "error";
			}
		}
	    /**
	     * CreatePublicGroup method
	     * @returns add result ok(error)
	     */
		public function  createPublicGroup($p_gname,$p_gdetail){
			$time = time();
			$sql = "INSERT INTO  `cdb_publicgroup` (
					`p_gid` ,
					`p_gname` ,
					`p_gdetail` ,
					`p_gpersons` ,
					`p_regman` ,
					`p_regtime` ,
					`p_status`
					)
					VALUES ('',  '$p_gname',  '$p_gdetail',  '',  '$this->user_id',  '$time',  '');";
			$result = mysql_query($sql);
			
			$newid = mysql_insert_id();
	    	$sql = "INSERT INTO  `cdb_publicgmember` (`pgid` ,`uid`) VALUES ('$newid',  '$this->user_id');";
			$result = mysql_query($sql);
			
			mysql_close();
			
			if($result){
				return "ok";
			}else {
				return "error";
			}
		}
	    /**
	     * CreatePublicGroup method
	     * @returns add result ok(error)
	     */
	    public  function  PublicGroupAddUser($gid,$uid){
	    	$sql = "INSERT INTO  `cdb_publicgmember` (`pgid` ,`uid`) VALUES ('$gid',  '$uid');";
			$result = mysql_query($sql);
			
			mysql_close();
			
			if($result){
				return "ok";
			}else {
				return "error";
			}
	    }
	    /**
	     * UserAddPublicGroup method
	     * @returns add result ok(error)
	     */
	    public  function  UserAddPublicGroup($gid){
	    	$uid = $this->user_id;
	    	
	    	$sql = "select `uid` from `cdb_publicgmember` where `pgid`=$gid AND `uid`=$uid";
	    	$result = mysql_query($sql);
	    	$row = mysql_fetch_array($result,MYSQL_ASSOC);
	    	
	    	if($row['uid'] == null){
		    	$sql = "INSERT INTO  `cdb_publicgmember` (`pgid` ,`uid`) VALUES ('$gid',  '$uid');";
	    	}else{
	    		$sql = "UPDATE `cdb_publicgmember` SET `last_id` = '0' WHERE `pgid` =$gid AND `uid` =$uid LIMIT 1";
	    	}
	    	
			$result = mysql_query($sql);

			mysql_close();
			
			if($result){
				return "ok";
			}else {
				return "error";
			}
	    }
			/*
		 * MessageManage People List
		 * return a array
		 */
		public function getfList(){
            $sql ='SELECT DISTINCT cg.groupname,cg.groupid
					FROM  `cdb_customgroup` cg where cg.uid='.$this->user_id;
					
            $result = mysql_query($sql);
			
			$g = array();
			while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
				$group = new CustomGroup();
				$group->data = $row['groupid'];
				$group->gname = $row['groupname'];
				$group->type = "root";
			 	
				$xmlstr = $xmlstr."<node label='我的好友' icon='tran' type='root'>"."<node label='$group->gname' icon='tran' type='$group->type' data='$group->data' ms=''>";
				
				$sql1 = "SELECT m. *
							FROM cdb_members m
							LEFT JOIN cdb_memberfields mf ON mf.uid = m.uid
							WHERE m.uid
							IN (SELECT buddyid FROM  `cdb_customgpmember` WHERE  `uid` =$this->user_id AND `groupid` =$group->data and `cp_type`=3)";
				$result1 = mysql_query($sql1);
				$p = array();
				while ($row1 = mysql_fetch_array($result1,MYSQL_ASSOC)){
//Format data to object 
					$person = new Person();

					$person->username = $row1['username'];
					$person->email = $row1['email'];
					$person->bday = $row1['bday'];
					$person->gender = $row1['gender'];
					$person->bio = split('			',$row1['bio']);
					$person->bio = $person->bio[0];
					$person->sightml = $row1['sightml'];
					$person->site = $row1['site'];
					$person->location = $row1['location'];
					$person->qq = $row1['qq'];
					$person->ms = 'red';
					$person->invisible = $row1['invisible'];
					$person->uid = $row1['uid'];

					switch($person->invisible){
						case 0:
						$person->icon = "online";
						break;
						case 1:
						$person->icon = "hide";
						break;
						case 2:
						$person->icon = "busy";
						break;
						case 3:
						$person->icon = "leave";
						break;
						
						default:
						$person->icon = "online";
						break;
					}

					$p[] = $person;
					$xmlstr = $xmlstr."<node label='$person->username' icon='$person->icon' type='people' data='$person->uid' parent='$group->data' ms=''></node>";
				}
				
				$group->children = $p;
				$g[] = $group;
				
				$xmlstr = $xmlstr."<node icon='tran' data='tran'/></node>";
			}
			$sql2 = "SELECT m.*  
						FROM cdb_members m
						LEFT JOIN cdb_memberfields mf ON mf.uid = m.uid
						WHERE m.uid
						IN (SELECT buddyid FROM  `cdb_customgpmember` WHERE  `uid` =$this->user_id AND `groupid` =0 and `cp_type`=3)";
			$result2 = mysql_query($sql2);
			$p = array();
			while ($row1 = mysql_fetch_array($result2,MYSQL_ASSOC)){
//Format data to object 
				$person = new Person();

				$person->username = $row1['username'];
				$person->email = $row1['email'];
				$person->bday = $row1['bday'];
				$person->gender = $row1['gender'];
				$person->bio = split('			',$row1['bio']);
				$person->bio = $person->bio[0];
				$person->sightml = $row1['sightml'];
				$person->site = $row1['site'];
				$person->location = $row1['location'];
				$person->qq = $row1['qq'];
				$person->ms = 'red';
				$person->invisible = $row1['invisible'];
				$person->uid = $row1['uid'];
				
				switch($person->invisible){
						case 0:
						$person->icon = "online";
						break;
						case 1:
						$person->icon = "hide";
						break;
						case 2:
						$person->icon = "busy";
						break;
						case 3:
						$person->icon = "leave";
						break;
						
						default:
						$person->icon = "online";
						break;
					}

				$g[] = $person;
				$xmlstr = $xmlstr."<node label='$person->username' icon='$person->icon' data='$person->uid' type='children' parent='0' ms=''></node>";
				}
			$xmlstr = $xmlstr."</node>";
			
			$sql1 = "SELECT DISTINCT pg.p_gid,pg.`p_gname` FROM `cdb_publicgroup` pg LEFT JOIN cdb_publicgmember pgm  ON pg.p_gid=pgm.pgid  where pg.p_regman=$this->user_id OR pgm.uid =$this->user_id";
			$result1 = mysql_query($sql1);
			
			$pgroup = new PublicGroup();
			$xmlstr1 = "<node label='群' icon='group'>";
			
			while ($row = mysql_fetch_array($result1, MYSQL_ASSOC)) {
				$pgroup->groupid = $row['p_gid'];
				$pgroup->groupname = $row['p_gname'];
//				$pgroup->groupdetail = $row['p_gdetail'];
//				$pgroup->grouppersons = $row['p_gpersons'];
//				$pgroup->regman = $row['p_regman'];
//				$pgroup->regtime = $row['p_regtime'];
//				$pgroup->status = $row['p_status'];
				
				$xmlstr1 = $xmlstr1."<node label='$pgroup->groupname' icon='group' data='$pgroup->groupid' ms='' type='pgroup' msnum='' >";
					$sql1 = "SELECT m. *
								FROM cdb_members m
								LEFT JOIN cdb_memberfields mf ON mf.uid = m.uid
								WHERE m.uid
								IN (SELECT uid FROM  `cdb_publicgmember` WHERE  `pgid` =$pgroup->groupid)";
					$result1 = mysql_query($sql1);
					while ($row1 = mysql_fetch_array($result1, MYSQL_ASSOC)) {
						$person = new Person();
						$person->username = $row1['username'];
						$person->email = $row1['email'];
						$person->bday = $row1['bday'];
						$person->gender = $row1['gender'];
						$person->bio = split('			',$row1['bio']);
						$person->bio = $person->bio[0];
						$person->sightml = $row1['sightml'];
						$person->site = $row1['site'];
						$person->location = $row1['location'];
						$person->qq = $row1['qq'];
						$person->ms = 'red';
						$person->invisible = $row1['invisible'];
						$person->uid = $row1['uid'];
	
					switch($person->invisible){
						case 0:
						$person->icon = "online";
						break;
						case 1:
						$person->icon = "hide";
						break;
						case 2:
						$person->icon = "busy";
						break;
						case 3:
						$person->icon = "leave";
						break;
						
						default:
						$person->icon = "online";
						break;
					}
						
						$xmlstr1 = $xmlstr1."<node label='$person->username' icon='$person->icon' parent='$pgroup->groupid' type='people' data='$person->uid' ms='red'></node>";
					}
				$xmlstr1 = $xmlstr1."</node>";
			}
			$xmlstr1 = $xmlstr1."</node>";
			return $xmlstr.$xmlstr1;
		}
	    /**
	     * changeGroupUser method
	     * @returns add result ok(error)
	     */
	    public  function  changeGroupUser($gid,$buddyid,$ngid){
	    	$sql = "UPDATE  `cdb_customgpmember` SET  `groupid` =  '$ngid' WHERE  `cdb_customgpmember`.`groupid` =$gid AND  `cdb_customgpmember`.`buddyid` =$buddyid";
			$result = mysql_query($sql);
			
			mysql_close();
			
			if($result){
				return "ok";
			}else {
				return "error";
			}
	    }
	    /**
	     * CheckUser method
	     * @returns add result ok(error)
	     */
	    public function CheckUser($username){
	    	$sql = "SELECT `uid` FROM `cdb_members` where username='$username'";
			$result = mysql_query($sql);
			
			mysql_close();
			$row = mysql_fetch_array($result,MYSQL_ASSOC);
			if(empty($row['uid'])){
				return "ok";
			}else {
				return "error";
			}
	    }
	    /**
	     * Register method
	     * @returns add result ok(error)
	     */
	    public function register($username,$passwd,$email){
	    	$password = md5($passwd);
	    	$sql = "INSERT INTO `cdb_members` (
				`uid` ,
				`username`,
				`password` ,
				`email` )
				VALUES (
				NULL ,'$username','$password','$email');";
			$result = mysql_query($sql);
			
			mysql_close();
			
			if($result){
				return "ok";
			}else {
				return "error";
			}
	    }
	    /**
	     * Change Public Group Name method
	     * @returns add result ok(error)
	     */
		public function changePGroupName($nname,$gid){
			$sql = "UPDATE `cdb_publicgroup` SET  `p_gname` =  '$nname' WHERE `p_gid` ='$gid' AND `p_regman`='$this->user_id' ;";
			
			$result = mysql_query($sql);

			mysql_close();
			if($result){
				return "ok";
			}else {
				return "error";
			}
		}
		/**
		 * get MySelf GroupList
		 * return a array
		 */
		public function getMyGroup(){
			$p = array();
			$sql = "SELECT `groupid` , `groupname` FROM `cdb_customgroup` WHERE `uid` =$this->user_id";
			$result = mysql_query($sql);
			
			while ($row = mysql_fetch_array($result,MYSQL_ASSOC)){
				$$object = array();
				$object['label'] = $row['groupname']; 
				$object['data'] = $row['groupid'];
				
				array_unshift($p,$object);
			}
			if(count($p)){
				return $p;
			}else{
				return $sql;//'error';
			}
		}
	    /**
	     * Get People Group List Name method
	     * @returns a array List
	     */
		public function getGroupPeopleList($gid){
			$sql = "SELECT m.* , mf.bio,mf.sightml,mf.qq,mf.location
						FROM cdb_members m
						LEFT JOIN cdb_memberfields mf ON mf.uid = m.uid
						WHERE m.uid
						IN (SELECT `uid` FROM  `cdb_publicgmember` WHERE `pgid` =$gid)";

			$result = mysql_query($sql);
			$p = array();
			while ($row = mysql_fetch_array($result,MYSQL_ASSOC)){
//Format data to object 
				$person = new Person();

				$person->username = $row['username'];
				$person->email = $row['email'];
				$person->bday = $row['bday'];
				$person->gender = $row['gender'];
				$person->bio = split('			',$row['bio']);
				$person->bio = $person->bio[0];
				$person->sightml = $row['sightml'];
				$person->site = $row['site'];
				$person->location = $row['location'];
				$person->qq = $row['qq'];
				$person->ms = 'red';
				$person->invisible = $row['invisible'];
				$person->uid = $row['uid'];
				
				switch($person->invisible){
						case 0:
						$person->icon = "online";
						break;
						case 1:
						$person->icon = "hide";
						break;
						case 2:
						$person->icon = "busy";
						break;
						case 3:
						$person->icon = "leave";
						break;
						
						default:
						$person->icon = "online";
						break;
					}

				$g[] = $person;
				$xmlstr = $xmlstr."<node label='$person->username' icon='$person->icon' data='$person->uid' type='children' parent='$group->data' ms=''></node>";
				}
			mysql_close();

			return $xmlstr;
		}
		/*
		 * delete customgroup
		 * return ok/error
		 */
		public function delCustomGroup($gid){
			$sql = "DELETE FROM `cdb_customgroup` WHERE uid =$this->user_id AND groupid =$gid";
			$result = mysql_query($sql);
			
			mysql_close();
			
			if($result){
				return "ok";
			}else{
				return "error";
			}
		}
		/*
		 * delete publicGroup
		 * return ok/error
		 */
		public function delPublicGroup($gid){
			$sql = "select p_regman from `cdb_publicgroup` where p_gid =$gid";
			$result = mysql_query($sql);
			$row = mysql_fetch_array($result,MYSQL_ASSOC);
			
			if($row['p_regman'] == $this->user_id){
				$sql = "DELETE FROM `cdb_publicgroup` WHERE p_regman =$this->user_id AND p_gid =$gid";
				$result = mysql_query($sql);
				
				$sql = "DELETE FROM `cdb_publicgmember` WHERE pgid =$gid";
				$result = mysql_query($sql);
				
				mysql_close();
				if($result){
					return "ok";
				}else{
					return "error";
				}
			}else{
				return "nopower";
			}
		}
		/*
		 * delete publicGroup
		 * return ok/error
		 */
		public function getGroupInfo($gid){
			$sql = "select p_gname,p_gdetail from `cdb_publicgroup` where p_gid =$gid";
			$result = mysql_query($sql);
			$row = mysql_fetch_array($result,MYSQL_ASSOC);
			
			$p = array();
			$p['p_gname'] = $row['p_gname'];
			$p['p_gdetail'] = $row['p_gdetail'];
			
			if($result){
				return $p;
			}else{
				return 'error';
			}
		}
	}
?>
